/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2023-02-23 15:38:06
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-02-23 16:57:16
 */

let util = require("../util");
// let operate = require("../db/opeate")
// let db = require("../db/index");
let { query } = require("../db/pool")
exports.add = (req, res) => {
  let params = req.body;
  params.id = util.uuidGenerator();
  let strSql = "insert into request_config(id,code,address,type,description,service,category,bind_data_item,params,callback_params,sort_number,create_author,last_modified_author) values('" + params.id + "' ,'" + params.code + "' ,'" + params.address + "' ,'" + params.type + "' ,'" + params.description + "' ,'" + params.service + "' ,'" + params.category + "' ,'" + params.bind_data_item + "' ,'" + params.params + "' ,'" + params.callback_params + "' ,'" + params.sort_number + "' ,'" + params.create_author + "' ,'" + params.create_author + "');";
  console.log("params ", params);
  requestConfigQuery(strSql, res);
}
exports.getList = (req, res) => {
  let params = req.body;
  let wh = 'where ';
  for (let key in params) {
    (!util.isEmpty(params[key])) ? wh += key + "='" + params[key] + "' and " : "";
  }
  wh = wh.substring(0, wh.lastIndexOf("and")) + ";";
  let strSql = "select * from request_config ";
  wh.trim() != "where" ? strSql += wh : strSql += ";";
  requestConfigQuery(strSql, res);
}
exports.edit = (req, res) => {
  let params = req.body;
  let strSql = "update request_config set code='" + params.code + "' ,description='" + params.description + "' ,sort_number='" + params.sort_number + "' ,last_modified_author='" + params.last_modified_author + "' ,address='" + params.address + "' ,type='" + params.type + "' ,service='" + params.service + "' ,category='" + params.category + "' ,bind_data_item='" + params.bind_data_item + "' ,params='" + params.params + "' ,callback_params='" + params.callback_params + "' where id='" + params.id + "';";
  requestConfigQuery(strSql, res);
}
exports.delete = (req, res) => {
  let params = req.query;
  let strSql = "delete from request_config where id='" + params.id + "';";
  // operate.delete(strSql, res);
  requestConfigQuery(strSql, res);
}

async function requestConfigQuery(sql, res) {
  console.log("sql ", sql);
  let rtn = await query(sql);
  res.send(rtn);
}